// pages/desk/desk.js
const Desk = require("../../lib/cardDesk.js");
const app = getApp();
var cloud_t=false;

Page({
    data: {
        desks: [],
        selected: -1,
        index: 0
    },
    onShow: function () {
        wx.setNavigationBarColor({
            frontColor: '#000000',
            backgroundColor: '#f7f7f7',
        })
        this.getGlobalDesk();
    },
    createDesk() {
        const openid = app.globalData.openid;
        let desk = new Desk.Desk(openid);
        app.globalData.desks.unshift(desk);
        this.getGlobalDesk();
    },
    getGlobalDesk() {
        this.setData({
            desks: app.globalData.desks
        })
    },
    changeName(e) {
        let index = e.target.dataset.num;
        app.globalData.desks[index].changeName(e.detail.value);
        this.getGlobalDesk()
    },
    goto(e) {
        if (this.data.menu) {
            this.hideMenu();
            return;
        }
        let index = e.currentTarget.dataset.num;
        wx.navigateTo({
            url: '../detail/detail?index=' + index,
        })
    },
    onPullDownRefresh() {
        this.getGlobalDesk();
        wx.stopPullDownRefresh();
    },
    showMenu(e) {
        let index = e.target.dataset.num;
        this.setData({
            selected: index,
            menu: true
        })
    },
    hideMenu(e) {
        this.setData({
            selected: -1,
            menu: false
        })
    },
    delDesk() {
        const that=this;
        wx.showModal({
            title: '请确认',
            content: '是否要删除该卡组？',
            showCancel: true,
            success(res) {
                if (res.confirm) {
                    wx.showLoading({
                        title: '正在删除',
                    });
                    let index = that.data.selected;
                    app.globalData.desks[index].suicide();
                    app.globalData.desks.splice(index, 1);
                    that.getGlobalDesk();
                    wx.hideLoading();                    
                } else if (res.cancel) {
                    return;
                }
            }
        })
    },
    toTop() {
        wx.showLoading({
            title: '正在移动',
        });
        let index = this.data.selected;
        const temp = app.globalData.desks[index];
        app.globalData.desks.splice(0,0,temp);
        app.globalData.desks.splice(index+1,1);
        app.globalData.desks[0].update();
        this.getGlobalDesk();
        this.setData({
            selected: 0
        });
        wx.hideLoading();
    },
    getCloudDesk(e) {
        this.setData({
            index: e.detail.current
        })
        if (e.detail.current==1) {
            const dk = require('../../lib/cardDesk.js');
            dk.getCloudDesk((res)=>{
                this.setData({
                    cloudDesks: res
                })
            });
        }
    },
    share(e) {
        let index = this.data.selected;
        app.globalData.desks[index].share();
        this.getGlobalDesk();
    }
})